package com.lagou.common;

import java.util.HashMap;
import java.util.Map;

public class InvokerContext {

    private static final ThreadLocal<Map<String, Object>> threadLocal = new ThreadLocal<Map<String, Object>>();

    public static void set(String key, Object value) {
        Map<String, Object> map = threadLocal.get();
        if (map == null) {
            map = new HashMap<String, Object>();
        }
        map.put(key, value);
        threadLocal.set(map);
    }

    public static void attach(Map<String, Object> values) {
        if (null == values || values.isEmpty()) {
            return;
        }
        threadLocal.set(values);
    }

    public static Object getValue(String key) {
        Map<String, Object> map = threadLocal.get();
        if (null == map || map.isEmpty()) {
            return null;
        }
        return map.get(key);
    }

    public static void clear() {
        threadLocal.remove();
    }
}
